#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/9/27 18:24
# @Author  : 朱宽
# @File    : intelligentWait.py
# @Software: Win10、Python3.8.5 and Pycharm
'''
显示等待，已经和ParseElementLocator结合，只用于参考学习。
定义了：
1、显示等待页面元素出现在DOM树种，但并不一定可见，存在则返回该页面元素对象
2、判断某个元素中是否可见并且是enable的，代表可单击
3、判断某个元素是否被选中了，一般用在下拉列表
4、检查iframe是否存在，存在则切换进去
5、显示等待页面元素的出现，并返回元素
'''
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class waitUtil(object):
    def __init__(self, driver):
        '''
        定义一个字典
        :param driver:
        '''
        self.locationTypeDict = {
            "xpath": By.XPATH,
            "id": By.ID,
            "name": By.NAME,
            "css_selector": By.CSS_SELECTOR,
            "class_name": By.CLASS_NAME,
            "tag_name": By.TAG_NAME,
            "link_text": By.LINK_TEXT,
            "partial_link_text": By.PARTIAL_LINK_TEXT
        }
        self.driver = driver
        self.wait = WebDriverWait(self.driver, 30)

    def presence_of_element_located(self, locationType, locatorExpression, *args):
        '''
        1、显示等待页面元素出现在DOM树中，但并不一定可见
        :param locationType:
        :param locatorExpression:
        :param args:
        :return:
        '''
        try:
            if locationType.lower() in self.locationTypeDict:
                self.wait.until(
                    EC.presence_of_element_located((self.locationTypeDict[locationType.lower()], locatorExpression)))
            else:
                raise TypeError(u"未找到定位方式，请确认定位方法是否正确")
        except Exception as e:
            raise e

    def clickable_of_element(self, locationType, locatorExpression, *args):
        '''
        2、判断某个元素中是否可见并且是enable的，代表可单击
        :param locationType:
        :param locatorExpression:
        :param args:
        :return:
        '''
        try:
            if locationType.lower() in self.locationTypeDict:
                self.wait.until(
                    EC.element_to_be_clickable((self.locationTypeDict[locationType.lower()], locatorExpression)))
            else:
                raise TypeError(u"未找到定位方式，请确认定位方法是否正确")
        except Exception as e:
            raise e

    def selection_of_element(self, locationType, locatorExpression, *args):
        '''
        3、判断某个元素是否被选中了，一般用在下拉列表
        :param locationType:
        :param locatorExpression:
        :param args:
        :return:
        '''
        try:
            if locationType.lower() in self.locationTypeDict:
                self.wait.until(
                    EC.element_to_be_selected((self.locationTypeDict[locationType.lower()], locatorExpression)))
            else:
                raise TypeError(u"未找到定位方式，请确认定位方法是否正确")
        except Exception as e:
            raise e

    def frame_to_be_available_and_switch_to_it(self, locationType, locatorExpression, *args):
        '''
        4、检查iframe是否存在，存在则切换进去
        :param locationType:
        :param locatorExpression:
        :param args:
        :return:
        '''
        try:
            if locationType.lower() in self.locationTypeDict:
                self.wait.until(EC.frame_to_be_available_and_switch_to_it(
                    (self.locationTypeDict[locationType.lower()], locatorExpression)))
            else:
                raise TypeError(u"未找到定位方式，请确认定位方法是否正确")
        except Exception as e:
            raise e

    def visibility_element_located(self, locationType, locatorExpression, *args):
        '''
        5、显示等待页面元素的出现，并返回元素
        :param locationType:
        :param locatorExpression:
        :param args:
        :return:
        '''
        driver = self.driver
        try:
            if locationType.lower() in self.locationTypeDict:
                element = self.wait.until(
                    lambda x: x.find_element(self.locationTypeDict[locationType.lower()], locatorExpression))
                return element
            else:
                raise TypeError(u"未找到定位方式，请确认定位方法是否正确")
        except Exception as e:
            raise e


if __name__ == '__main__':
    pass
